#include "Object.h"
#include "tinyxml.h"

Ogre::NameGenerator Object::objectNameGenerator = Ogre::NameGenerator("Object");

std::string Object::toString() const {
	return "Object";
}

template<class T>
bool Object::hasProperty(std::string name)
{
	Property<T>* property = dynamic_cast< Property<T>* >(
	                            properties.find(name)
	                        );
	if(property == NULL || property == properties.end())
		return false;
	return true;
}


std::string Object::getXmlString(int indent)
{
	std::stringstream ss;
	
	for(int i=0; i<indent; i++, ss << "\t");
	ss << "<Object ";
	ss << "name=\"" << getName() << "\"";
	if(properties.empty())
		{
			ss << ">" << std::endl;
			return ss.str();
		}
	ss << ">" << std::endl;

	for(PropertyMapIterator it = properties.begin(); it!=properties.end(); ++it)
		{
			//!TODO: properly skip on pointers
//			if(is_pointer<>::value) continue;
			if(it->second->isNull() || TypeInfo::getType(it->second->getTypeName())==NIL) continue;
			
			if(TypeInfo::getType(it->second->getTypeName())==OBJECT)
				{
					Object o = prop_cast_val<Object>(it->second);
					for(int i=0; i<indent; i++, ss << "\t");
					ss << o.getXmlString(indent+1);
				}
			else
				{
					for(int i=0; i<indent+1; i++, ss << "\t");
					ss << it->second->getXmlString();
				}
			ss << std::endl;
		}
	
	for(int i=0; i<indent; i++, ss << "\t");
	ss << "</Object>" << std::endl;
	return ss.str();
}

//Object Object::fromXmlString(const std::string& str)
//{
//}
